package view;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

import model.State;
import control.CConnection;
import control.CState;

public class ExtractStateData {
	public static void main(String args[]) {
		
		CConnection cn = new CConnection();
		
		try {
			cn.createConnection();
			CState CState = new CState(cn);
			ArrayList<State> states = CState.retrieveAll();

	    	BufferedWriter out = new BufferedWriter(new FileWriter("felipedavi/statedata.txt"));
	    	for (int i = 0; i < states.size(); i++) {
	    		Integer stateid = states.get(i).getId();
	    		out.write(stateid.toString() + " " + states.get(i).getName());
	    		out.newLine();
	        }
	    	
			out.close();
			
			System.out.println("File generated successfully.");
		}
		catch (ClassNotFoundException e) {
			System.err.println("Couldn't connect to Derby Database.");
		}
		catch (SQLException e) {
			System.err.println("Error in SQL Statement.");
		} catch (IOException e) {
			System.err.println("Error in I/O.");
		}


	}
}
